package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import es.ats.codington.festival.daos.EventDAOImpl;
import es.ats.codington.festival.daos.EventSignUpDAOImpl;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.EventSignUp;
import es.ats.codington.festival.interfaces.services.IEventSignUpService;
import es.ats.codington.festival.utils.FERSDataConnection;

public class EventSignUpService implements IEventSignUpService{

	Connection connection=null;
	
	/**
	 * 
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	
	public int registerVisitorForNewEvent(EventSignUp e) throws SQLException{
		
		int affectedRows = 0;

		try {
			
			connection = FERSDataConnection.getConnection();
			EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
			affectedRows = esup.registerVisitorForNewEventDAO(e);
		} catch(Exception ee) {
			System.out.println("registerNewVisitor: "+ ee.getMessage());
			}
		
			connection.close();

		return affectedRows;
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEventsofVisitor(EventSignUp e) throws SQLException{
		boolean bool = false;

		try {
			
			connection = FERSDataConnection.getConnection();
			EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
			bool = esup.checkEventsofVisitorDAO(e);
			
		} catch(Exception ee) {
			System.out.println("registerNewVisitor: "+ ee.getMessage());
			}
		
			connection.close();

		return bool;
	
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	public int unregisterVisitorForEvent(EventSignUp e) throws SQLException{
		int affectedRows = 0;
		
		try {
			
			connection = FERSDataConnection.getConnection();
			EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
			affectedRows = esup.unregisterVisitorForEventDAO(e);
			
		} catch(Exception ee) {
			System.out.println("registerNewVisitor: "+ ee.getMessage());
			}
		
			connection.close();
		
		return affectedRows;
	}
	
	/**
	 * @param e
	 * @return
	 * @throws SQLException
	 */
	
	public List<Event> viewEventsOfUser(EventSignUp e) throws SQLException {
		List<Event> events = new ArrayList<Event>();
		
		try {
			
			connection = FERSDataConnection.getConnection();
			EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
			events = esup.viewEventsOfUserDAO(e);
			
		} catch(Exception ee) {
			System.out.println("registerNewVisitor: "+ ee.getMessage());
			}
		
			connection.close();
		
		return events;
	}
	
public boolean updateMinusSeatsAvailableEvent(EventSignUp e) throws SQLException{
		
		boolean bool=false;
		try{	
			
			connection = FERSDataConnection.getConnection();
			EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
			bool=esup.updateMinusSeatsAvailableEventDAO(e);
			
			
		} catch (Exception ee) {
			ee.getMessage();
			return bool;

		} 
		
		connection.close();
		return bool;
		
	}
	
public boolean updateSumSeatsAvailableEvent(EventSignUp e) throws SQLException{
	
	boolean bool=false;
	try{	
		
		connection = FERSDataConnection.getConnection();
		EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
		bool=esup.updateSumSeatsAvailableEventDAO(e);
		
		
	} catch (Exception ee) {
		ee.getMessage();
		return bool;

	} 
	
	connection.close();
	return bool;
	
}
public boolean checkSeatsAvailable(EventSignUp e) throws SQLException{
	
	boolean bool=false;
	try{	
		
		connection = FERSDataConnection.getConnection();
		EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
		bool=esup.checkSeatsAvailableDAO(e);
		
		
	} catch (Exception ee) {
		ee.getMessage();
		return bool;

	} 
	
	connection.close();
	return bool;
	
}

public boolean unregisterAllVisitorforEvent(int idEvent) throws SQLException, ClassNotFoundException{
	boolean bool=false;
	try{
		connection = FERSDataConnection.getConnection();
		EventSignUpDAOImpl esup=new EventSignUpDAOImpl(connection);
		bool = esup.unregisterAllVisitorforEvent(idEvent);
	}catch(Exception e) {
		System.out.println("updateInfo: "+ e.getMessage());
	}
	connection.close();
	return bool;
}


	
	
}
